<?php
/*
Copyright (C) 2009 de Flotte Maxence

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
?>
<?php
session_start();
if(!empty($_GET['action'])) {
	switch(strToLower($_GET['action'])) {
		case 'regendom':
			include './ZzDev/genDomain.php';
			$_SESSION['msg'] = getAndTranslateDomains();
			header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
			break;
		case 'gendom':
			include './ZzDev/genDomain.php';
			$_SESSION['msg'] = getAndTranslateDomains(true);
			header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
			break;
			case 'domdata':
			include './ZzDev/genDomain.php';
			$_SESSION['msg'] = getAndTranslateDomains(true);
			$_SESSION['msg'] .= getAndPutDomDatas();
			header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
			break;
		case 'cleancache':
			rmdir_recurse('./ZzG/');
			rmdir_recurse('./ZzStore/');
			rmdir_recurse('./ZzGroup/');
			rmdir_recurse('./ZzView/gen/');
			rmdir_recurse('./ZzS/gen/');
			rmdir_recurse('./ZzT/gen/');
			$_SESSION['msg'] = 'Cache files are empty';
			header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
			break;
		case 'devtool':
			include './ZzDev/devTool.php';
			break;
		default :
			$_SESSION['msg'] = 'Unknown commande';
			header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
			break;
	}
}
else {
	showPage();
}

function rmdir_recurse($path) {
	$path= rtrim($path, '/').'/';
	$handle = opendir($path);
	while(false !== ($file = readdir($handle)))
		if($file != "." and $file != "..") {
			$fullpath= $path.$file;
			if(is_dir($fullpath)) {
				rmdir_recurse($fullpath);
				rmdir($fullpath);
			}
			else
				unlink($fullpath);
		}
	closedir($handle);
}
?>
<?php
function showPage() {
	if(isSet($_SESSION['msg']))
		$msg = '<p style="border: 2px solid green; color: green; background-color: #AAFFAA; padding: 1%; font-weight: bold; position: absolute; bottom: 10px; left: 1.5%; width: 95%; overflow: auto; max-height: 80%;" onclick="">'.$_SESSION['msg'].'<span onclick="this.parentNode.parentNode.removeChild(this.parentNode);" style="position: fixed; bottom: 35px; right: 3%; font-weight: lighter; font-size: 10px; ">Click me to hide this message</span></p>';
	else
		$msg = '';
	unset($_SESSION['msg']);
?>
<html>
	<head>
		<title>Developer Page</title>
	</head>
	<body>
		<h1>Developer Page</h1>
		<?php echo $msg; ?>
		<p>
			<a href="dev.php?action=regenDom">regenerate domains</a><br/>
			<a href="dev.php?action=genDom" onclick="return confirm('Warning :: this action will remove all the data on the database. Are you sure you want to continue ?');">remove and regenerate all domains</a><br/>
			<a href="dev.php?action=domData" onclick="return confirm('Warning :: this action will remove all the data on the database. Are you sure you want to continue ?');">Put DomData on database</a><br/>
			<a href="dev.php?action=devTool">Use the devTool</a><br/>
			<a href="dev.php?action=cleanCache">Empty Cache files</a><br/>
		</p>
	</body>
</html>
<?php
}
?>